package com.talkingdata.hadoop.guard.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Controller
public class UploadController {
    private static String UPLOADED_FOLDER = ".\\conf\\";

    @GetMapping("/upload")
    public String upload(){
        return "upload";
    }

    @GetMapping("/uploadStatus")
    public String uploadStatus(){
        return "uploadStatus";
    }

    @RequestMapping(value = "/singleFileUpload.do")
    public String singleFileUpload(
            @RequestParam("file") MultipartFile file,
            RedirectAttributes redirectAttributes
    ) {
        String fileName = file.getOriginalFilename();
        if (file.isEmpty() || fileName.endsWith("sh")) {
            redirectAttributes.addFlashAttribute("message","Please select a file to upload");
            System.out.println("文件不为空or格式不正确");
            return "redirect:upload";
        }

        try {
            byte [] bytes = file.getBytes();
            Path path = Paths.get(UPLOADED_FOLDER + fileName);
            Files.write(path,bytes);
            redirectAttributes.addFlashAttribute("message","You successfully uploaded '" + fileName + "'");
        } catch (Exception e) {
            e.printStackTrace();
            redirectAttributes.addFlashAttribute("message","You failed uploaded '" + fileName + "'");
            System.out.println("上传失败！");
            return "redirect:/upload";
        }
        return "redirect:/uploadStatus";
    }


    @PostMapping("/singleFileUpload1.do")
    public String singleFileUpload1(MultipartFile file, HttpSession session, Model model){
        System.out.println("filename : " + file.getOriginalFilename());
        if (!file.isEmpty()) {
            try {
                // 获取上传文件的原始名称
                //String fileName = System.currentTimeMillis() + photo.getOriginalFilename();
                String fileName = file.getOriginalFilename();
                if (fileName.endsWith("xml")) {
                    // 文件上传到服务器的位置
                    // String path = "D:\\NightSchool\\images";
                    //String path = session.getServletContext().getRealPath("/static/conf");

                    File fileInfo = new File("./", fileName);
                    System.out.println("fileInfo : " + fileInfo.getPath());
                    // 文件上传
                    file.transferTo(fileInfo);
                    model.addAttribute("message", "文件上传成功");
                } else {
                    model.addAttribute("message", "文件类型不正确");
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            model.addAttribute("message", "没有选择要上传的文件");
        }
        return "redirect:/uploadStatus";
    }
}
